using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace LMSData
{
	/// <summary>
	/// Strongly-typed collection for the Message class.
	/// </summary>
	[Serializable]
	public partial class MessageCollection : ActiveList<Message, MessageCollection> 
	{	   
		public MessageCollection() {}

	}

	/// <summary>
	/// This is an ActiveRecord class which wraps the Message table.
	/// </summary>
	[Serializable]
	public partial class Message : ActiveRecord<Message>
	{
		#region .ctors and Default Settings
		
		public Message()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}

		
		private void InitSetDefaults() { SetDefaults(); }

		
		public Message(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}

		public Message(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}

		 
		public Message(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}

		
		protected static void SetSQLProps() { GetTableSchema(); }

		
		#endregion
		
		#region Schema and Query Accessor
		public static Query CreateQuery() { return new Query(Schema); }

		
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}

		}

		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("Message", TableType.Table, DataService.GetInstance("LMS"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarMessageID = new TableSchema.TableColumn(schema);
				colvarMessageID.ColumnName = "MessageID";
				colvarMessageID.DataType = DbType.Int32;
				colvarMessageID.MaxLength = 0;
				colvarMessageID.AutoIncrement = true;
				colvarMessageID.IsNullable = false;
				colvarMessageID.IsPrimaryKey = true;
				colvarMessageID.IsForeignKey = false;
				colvarMessageID.IsReadOnly = false;
				colvarMessageID.DefaultSetting = @"";
				colvarMessageID.ForeignKeyTableName = "";
				schema.Columns.Add(colvarMessageID);
				
				TableSchema.TableColumn colvarParentID = new TableSchema.TableColumn(schema);
				colvarParentID.ColumnName = "ParentID";
				colvarParentID.DataType = DbType.Int32;
				colvarParentID.MaxLength = 0;
				colvarParentID.AutoIncrement = false;
				colvarParentID.IsNullable = true;
				colvarParentID.IsPrimaryKey = false;
				colvarParentID.IsForeignKey = false;
				colvarParentID.IsReadOnly = false;
				colvarParentID.DefaultSetting = @"";
				colvarParentID.ForeignKeyTableName = "";
				schema.Columns.Add(colvarParentID);
				
				TableSchema.TableColumn colvarMessageTitle = new TableSchema.TableColumn(schema);
				colvarMessageTitle.ColumnName = "MessageTitle";
				colvarMessageTitle.DataType = DbType.String;
				colvarMessageTitle.MaxLength = 255;
				colvarMessageTitle.AutoIncrement = false;
				colvarMessageTitle.IsNullable = true;
				colvarMessageTitle.IsPrimaryKey = false;
				colvarMessageTitle.IsForeignKey = false;
				colvarMessageTitle.IsReadOnly = false;
				colvarMessageTitle.DefaultSetting = @"";
				colvarMessageTitle.ForeignKeyTableName = "";
				schema.Columns.Add(colvarMessageTitle);
				
				TableSchema.TableColumn colvarUserID = new TableSchema.TableColumn(schema);
				colvarUserID.ColumnName = "UserID";
				colvarUserID.DataType = DbType.Int32;
				colvarUserID.MaxLength = 0;
				colvarUserID.AutoIncrement = false;
				colvarUserID.IsNullable = true;
				colvarUserID.IsPrimaryKey = false;
				colvarUserID.IsForeignKey = true;
				colvarUserID.IsReadOnly = false;
				colvarUserID.DefaultSetting = @"";
				
					colvarUserID.ForeignKeyTableName = "User";
				schema.Columns.Add(colvarUserID);
				
				TableSchema.TableColumn colvarThreadID = new TableSchema.TableColumn(schema);
				colvarThreadID.ColumnName = "ThreadID";
				colvarThreadID.DataType = DbType.Int32;
				colvarThreadID.MaxLength = 0;
				colvarThreadID.AutoIncrement = false;
				colvarThreadID.IsNullable = true;
				colvarThreadID.IsPrimaryKey = false;
				colvarThreadID.IsForeignKey = true;
				colvarThreadID.IsReadOnly = false;
				colvarThreadID.DefaultSetting = @"";
				
					colvarThreadID.ForeignKeyTableName = "Thread";
				schema.Columns.Add(colvarThreadID);
				
				TableSchema.TableColumn colvarSequence = new TableSchema.TableColumn(schema);
				colvarSequence.ColumnName = "Sequence";
				colvarSequence.DataType = DbType.Int32;
				colvarSequence.MaxLength = 0;
				colvarSequence.AutoIncrement = false;
				colvarSequence.IsNullable = true;
				colvarSequence.IsPrimaryKey = false;
				colvarSequence.IsForeignKey = false;
				colvarSequence.IsReadOnly = false;
				colvarSequence.DefaultSetting = @"";
				colvarSequence.ForeignKeyTableName = "";
				schema.Columns.Add(colvarSequence);
				
				TableSchema.TableColumn colvarMessageX = new TableSchema.TableColumn(schema);
				colvarMessageX.ColumnName = "Message";
				colvarMessageX.DataType = DbType.String;
				colvarMessageX.MaxLength = 2147483647;
				colvarMessageX.AutoIncrement = false;
				colvarMessageX.IsNullable = true;
				colvarMessageX.IsPrimaryKey = false;
				colvarMessageX.IsForeignKey = false;
				colvarMessageX.IsReadOnly = false;
				colvarMessageX.DefaultSetting = @"";
				colvarMessageX.ForeignKeyTableName = "";
				schema.Columns.Add(colvarMessageX);
				
				TableSchema.TableColumn colvarCreatedOn = new TableSchema.TableColumn(schema);
				colvarCreatedOn.ColumnName = "CreatedOn";
				colvarCreatedOn.DataType = DbType.DateTime;
				colvarCreatedOn.MaxLength = 0;
				colvarCreatedOn.AutoIncrement = false;
				colvarCreatedOn.IsNullable = true;
				colvarCreatedOn.IsPrimaryKey = false;
				colvarCreatedOn.IsForeignKey = false;
				colvarCreatedOn.IsReadOnly = false;
				colvarCreatedOn.DefaultSetting = @"";
				colvarCreatedOn.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCreatedOn);
				
				TableSchema.TableColumn colvarIsDeleted = new TableSchema.TableColumn(schema);
				colvarIsDeleted.ColumnName = "IsDeleted";
				colvarIsDeleted.DataType = DbType.Boolean;
				colvarIsDeleted.MaxLength = 0;
				colvarIsDeleted.AutoIncrement = false;
				colvarIsDeleted.IsNullable = true;
				colvarIsDeleted.IsPrimaryKey = false;
				colvarIsDeleted.IsForeignKey = false;
				colvarIsDeleted.IsReadOnly = false;
				colvarIsDeleted.DefaultSetting = @"";
				colvarIsDeleted.ForeignKeyTableName = "";
				schema.Columns.Add(colvarIsDeleted);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["LMS"].AddSchema("Message",schema);
			}

		}

		#endregion
		
		#region Props
		
		  
		[XmlAttribute("MessageID")]
		public int MessageID 
		{
			get { return GetColumnValue<int>("MessageID"); }

			set { SetColumnValue("MessageID", value); }

		}

		  
		[XmlAttribute("ParentID")]
		public int? ParentID 
		{
			get { return GetColumnValue<int?>("ParentID"); }

			set { SetColumnValue("ParentID", value); }

		}

		  
		[XmlAttribute("MessageTitle")]
		public string MessageTitle 
		{
			get { return GetColumnValue<string>("MessageTitle"); }

			set { SetColumnValue("MessageTitle", value); }

		}

		  
		[XmlAttribute("UserID")]
		public int? UserID 
		{
			get { return GetColumnValue<int?>("UserID"); }

			set { SetColumnValue("UserID", value); }

		}

		  
		[XmlAttribute("ThreadID")]
		public int? ThreadID 
		{
			get { return GetColumnValue<int?>("ThreadID"); }

			set { SetColumnValue("ThreadID", value); }

		}

		  
		[XmlAttribute("Sequence")]
		public int? Sequence 
		{
			get { return GetColumnValue<int?>("Sequence"); }

			set { SetColumnValue("Sequence", value); }

		}

		  
		[XmlAttribute("MessageX")]
		public string MessageX 
		{
			get { return GetColumnValue<string>("Message"); }

			set { SetColumnValue("Message", value); }

		}

		  
		[XmlAttribute("CreatedOn")]
		public DateTime? CreatedOn 
		{
			get { return GetColumnValue<DateTime?>("CreatedOn"); }

			set { SetColumnValue("CreatedOn", value); }

		}

		  
		[XmlAttribute("IsDeleted")]
		public bool? IsDeleted 
		{
			get { return GetColumnValue<bool?>("IsDeleted"); }

			set { SetColumnValue("IsDeleted", value); }

		}

		
		#endregion
		
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a Thread ActiveRecord object related to this Message
		/// 
		/// </summary>
		public LMSData.Thread Thread
		{
			get { return LMSData.Thread.FetchByID(this.ThreadID); }

			set { SetColumnValue("ThreadID", value.ThreadID); }

		}

		
		
		/// <summary>
		/// Returns a User ActiveRecord object related to this Message
		/// 
		/// </summary>
		public LMSData.User User
		{
			get { return LMSData.User.FetchByID(this.UserID); }

			set { SetColumnValue("UserID", value.UserID); }

		}

		
		
		#endregion
		
		
		
		//no ManyToMany tables defined (0)
		
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(int? varParentID,string varMessageTitle,int? varUserID,int? varThreadID,int? varSequence,string varMessageX,DateTime? varCreatedOn,bool? varIsDeleted)
		{
			Message item = new Message();
			
			item.ParentID = varParentID;
			
			item.MessageTitle = varMessageTitle;
			
			item.UserID = varUserID;
			
			item.ThreadID = varThreadID;
			
			item.Sequence = varSequence;
			
			item.MessageX = varMessageX;
			
			item.CreatedOn = varCreatedOn;
			
			item.IsDeleted = varIsDeleted;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varMessageID,int? varParentID,string varMessageTitle,int? varUserID,int? varThreadID,int? varSequence,string varMessageX,DateTime? varCreatedOn,bool? varIsDeleted)
		{
			Message item = new Message();
			
				item.MessageID = varMessageID;
				
				item.ParentID = varParentID;
				
				item.MessageTitle = varMessageTitle;
				
				item.UserID = varUserID;
				
				item.ThreadID = varThreadID;
				
				item.Sequence = varSequence;
				
				item.MessageX = varMessageX;
				
				item.CreatedOn = varCreatedOn;
				
				item.IsDeleted = varIsDeleted;
				
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		#endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string MessageID = @"MessageID";
			 public static string ParentID = @"ParentID";
			 public static string MessageTitle = @"MessageTitle";
			 public static string UserID = @"UserID";
			 public static string ThreadID = @"ThreadID";
			 public static string Sequence = @"Sequence";
			 public static string MessageX = @"Message";
			 public static string CreatedOn = @"CreatedOn";
			 public static string IsDeleted = @"IsDeleted";
						
		}

		#endregion
	}

}

